package com.zw.cloud.mybatis.plus.controller.stenter.report;


import com.zw.cloud.mybatis.plus.entity.stenter.report.dto.ProductReportCountQueryDTO;
import com.zw.cloud.mybatis.plus.entity.stenter.report.vo.ReportProductCountVO;
import com.zw.cloud.mybatis.plus.service.api.stenter.report.IReportProductCountService;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 定型产量统计报表 前端控制器
 * </p>
 *
 * @author zw
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/report/report-product-count")
public class ReportProductCountController {

    @Resource
    private IReportProductCountService reportProductCountService;

    /**
     * 定型产量统计
     */
    @PostMapping("/queryProductReportCount")
    //http://127.0.0.1:8082/report/report-product-count/queryProductReportCount
    public ReportProductCountVO queryProductReportCount(@Validated @RequestBody ProductReportCountQueryDTO productReportCountQueryDTO) {
        return reportProductCountService.queryProductReportCount(productReportCountQueryDTO);
    }

    /**
     * 定型产量统计导出
     */
    @PostMapping("/exportProductReportCount")
    //http://127.0.0.1:8082/report/report-product-count/exportProductReportCount
    public void exportProductReportCount(@Validated @RequestBody ProductReportCountQueryDTO productReportCountQueryDTO, HttpServletResponse response) throws IOException {
        SXSSFWorkbook workbook = reportProductCountService.exportProductReportCount(productReportCountQueryDTO);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode("product_count.xlsx", String.valueOf(StandardCharsets.UTF_8)));
        workbook.write(response.getOutputStream());
    }


}
